package com.example.ui.share;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.entity.CircleMember;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.util.BitmapLoad;
import com.example.util.Util;

public class CircleMemberActivity extends Activity {
	public static final int FAIL = 0;
	public static final int SUCCESS = 1;
	public static Handler handler;
	private LinearLayout titleLayout;
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private ListView listview;
	private CircleMemberAdapter adapter;
	private Intent myIntent;
	private String circleId;
	private ArrayList<CircleMember> circleMemberList = new ArrayList<CircleMember>();
	private String memberDada;
	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_tf);
		myIntent = getIntent();
		circleId = myIntent.getStringExtra("circleId");
		findView();
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case FAIL:
					Util.toast(CircleMemberActivity.this, "网络连接失败");
					break;
				case SUCCESS:
					listView();
					break;

				default:
					break;
				}
				super.handleMessage(msg);
			}
		};
	}

	private void findView() {
		titleLayout = (LinearLayout)findViewById(R.id.ai_top_layout);
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleText = (TextView)findViewById(R.id.layout_title_middle_text);
		titleRightBtn = (Button)findViewById(R.id.layout_title_right_btn);
		listview = (ListView)findViewById(R.id.at_lv);
		init();
	}

	private void init() {
		titleLayout.setVisibility(View.VISIBLE);
		titleLeftBtn.setText("返回");
		titleText.setText("成员刘表");
		titleRightBtn.setVisibility(View.INVISIBLE);
		click();
	}
	
	private void click() {
		titleLeftBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		getCircleMember();
	}
	
	/**
	 * 圈子成员
	 */
	private void getCircleMember() {
		memberDada = NetRequest.getCircleMember(circleId,HSApplication.sp.getString("userid",""));
		String result = "" ;
		JSONObject jo;
		try {
			jo = new JSONObject(memberDada);
			result = jo.getString("response");
			if (result.equals("true")) {
				handler.sendEmptyMessage(SUCCESS);
			} else {
				handler.sendEmptyMessage(FAIL);
			}
		} catch (JSONException e) {
			handler.sendEmptyMessage(FAIL);
			e.printStackTrace();
		}
	}

	private void listView() {
		circleMemberList = AnalyzingJson.getCircleMember(memberDada);
		adapter = new CircleMemberAdapter();
		listview.setAdapter(adapter);
	}
	
	class CircleMemberAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return circleMemberList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return circleMemberList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			MemberHolder holder = null;
			if(convertView == null){
				convertView = LayoutInflater.from(CircleMemberActivity.this).inflate(R.layout.circle_member_list_item, null);
				holder = new MemberHolder();
				holder.headImg = (ImageView)convertView.findViewById(R.id.circle_member_list_item_img);
				holder.name = (TextView)convertView.findViewById(R.id.circle_member_list_item_name);
				holder.zhiwu = (TextView)convertView.findViewById(R.id.circle_member_list_item_zhiwu);
				holder.supply = (TextView)convertView.findViewById(R.id.circle_member_list_item_supply);
				holder.need = (TextView)convertView.findViewById(R.id.circle_member_list_item_need);
			}else{
				holder = (MemberHolder) convertView.getTag();
			}
			
			String imgStr = circleMemberList.get(position).getHeadPic().toString();
			if(!"".equals(imgStr)||imgStr!=null){
				holder.headImg.setImageBitmap(BitmapLoad.loadBitmap(CircleMemberActivity.this, imgStr));
			}
			holder.name.setText(circleMemberList.get(position).getUserName().toString());
			holder.zhiwu.setText(circleMemberList.get(position).getZhiwu().toString());
			holder.supply.setText(circleMemberList.get(position).getSupply().toString());
			holder.need.setText(circleMemberList.get(position).getRequirement().toString());
			
			return convertView;
		}
		
		class MemberHolder{
			ImageView headImg;
			TextView name;
			TextView zhiwu;//职务
			TextView supply;
			TextView need;
		}
		
	}

}
